import logging

import pytest

from resource.pom.contacts import Contacts
from resource.pom.keyboard import Keyboard


@pytest.fixture(autouse=True)
def remove_contactor(device):
    logging.info('方法前置----')
    Contacts.delete_contactor(device)
    yield
    logging.info('方法后置----删除联系人')
    Contacts.delete_contactor(device)


class Test:
    app = Contacts
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        "新建联系人，输入姓名。公司。手机号能正常保存"
        logging.info('预置条件')
        logging.info('步骤1：进入联系人应用, 点击“新建联系人”或“+”')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.menu_contacts)
        device.click(*self.app.add_contact_btn)
        logging.info('预期结果1.进入联系人编辑页面')
        device.refresh_layout()
        device.assert_text_exist('保存至: 手机')
        device.assert_element_exist(condition={'hint': '姓名'})
        device.assert_element_exist(condition={'hint': '公司'})
        device.assert_element_exist(condition={'hint': '电话号码'})

        logging.info('步骤2.输入姓名、公司、手机(电话号码)、私人(电子邮件)、备注、添加更多项等信息')
        device.click_element(device.get_element_by_condition(condition={'hint': '姓名'}))
        logging.info('预期结果2.点击唤醒键盘，可正常输入各项信息')
        device.assert_soft_keyboard_on()
        device.click(*self.keyboard.key_a)
        device.click(*self.keyboard.close)

        company_area = device.get_element_by_condition(condition={'hint': '公司'})
        device.click_element(company_area)
        device.assert_soft_keyboard_on()
        device.click(*self.keyboard.key_c)
        device.click(*self.keyboard.close)

        device.refresh_layout()
        number_area = device.get_element_by_condition(condition={'hint': '电话号码'})
        device.click_element(number_area)
        device.assert_soft_keyboard_on()
        device.click(*self.keyboard.key_0)
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.close)

        logging.info('步骤3.点击右上角√标识保存')
        device.click_element(device.get_element_by_key('AddContact_OK'))
        logging.info('预期结果3.保存成功，在联系人界面按首字母排序显示')
        device.refresh_layout()
        device.assert_text_exist(self.app.test_name)
        device.assert_text_exist(self.app.test_company)
        device.assert_text_exist(self.app.test_number)
        device.assert_text_exist('收藏')
        device.assert_text_exist('编辑')
        device.assert_text_exist('更多')
        device.go_back()
        device.refresh_layout()
        device.assert_text_exist(self.app.test_name)